How to Properly Close a File in Python with If and For Statements